﻿using System;
using System.IO;
using System.Linq;
using System.Management;
using System.Reflection;
using System.Threading.Tasks;
using log4net;
using log4net.Core;
using log4net.Layout;
using log4net.Layout.Pattern;


namespace Mofang.Utils.LogProvider
{
    /// <summary>
    /// 日志填充
    /// <para>--------------------------</para>
    /// <para>说明:IIS中不记录日志时，需自建 Logs 存放目录,然后给Users组加入修改|写入权限 </para>
    /// <para>@Author: cnalexi@gmail.com</para> 
    /// <para>@Create: 2012/5/17</para> 
    /// </summary>
    public class LogProvider
    {
        private static volatile ILog _logger;// = log4net.LogManager.GetLogger("PmsLog");

        private static readonly object lockThis = new object();

        private static void InitOperateLog()
        {
            //需要在AssemblyInfo.cs 文件最后一行注册Log4Net.config文件
            //[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config",Watch=true)]

            string path = @"log4net.config".MapRootPath();

            //var assemblyPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

            log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(path));

            //log4net.Config.XmlConfigurator.Configure();

            _logger = LogManager.Exists("CommonLogger");
        }

        private static ILog Logger
        {
            get
            {
                if (_logger == null)
                {
                    lock (lockThis)
                    {
                        if (_logger == null)
                        {
                            InitOperateLog();
                        }
                    }
                }
                return _logger;
            }

        }

        public static void DebugLog(object msg)
        {
            Logger.Debug(msg);
        }

        public static void DebugLog(object msg, Exception ex)
        {
            Logger.Debug(msg, ex);
        }

        public static void InfoLog(LogModel log)
        {
            try
            {
                if (log.Helper == null)
                {
                    log.Helper = new LogHelper();
                }
                log.Helper.SaveLog(log);

            }
            catch (Exception ex)
            {
                ErrorLog(ex);
                throw ex;
            }

        }

        public static void InfoLog(LogModel log, Exception ex)
        {
            //拼接
            string errmsg = string.Format("{0}_ERROR:{1}", log.Message, ex.Message);

            Task.Factory.StartNew(() =>
            {
                try
                {
                    log.Message = errmsg;
                    //Logger.Info(errmsg, ex);
                    if (log.Helper == null)
                    {
                        log.Helper = new LogHelper();
                    }

                    log.Helper.SaveLog(log);
                }
                catch (Exception error)
                {
                    ErrorLog(error);
                }
            });
        }

        public static void WarnLog(object msg)
        {
            Logger.Warn(msg);
        }

        public static void WarnLog(object msg, Exception ex)
        {
            Logger.Warn(msg, ex);
        }

        public static void ErrorLog(object msg)
        {
            Logger.Error(msg);
        }

        public static void ErrorLog(object msg, Exception ex)
        {
            Logger.Error(msg, ex);
        }

        public static void FatalLog(object msg)
        {
            Logger.Fatal(msg);
        }

        public static void FatalLog(object msg, Exception ex)
        {
            Logger.Fatal(msg, ex);
        }

    }

}
